<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="e" uri="/easytogo-tags"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>新用户</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0"> 
	<e:base/>   
	<script type="text/javascript">
		var id="";
		var originalEmail = "";
		window.onload = function(){
			var roleIds = "${roleIds}".split(",");
			var usergroup_id = "${usergroup_id}";
			$("input[type='radio'][name='usergroup_id'][value='"+usergroup_id+"']").attr("checked","checked");
			for(i=0;i<roleIds.length;i++){
        		$("input[type='checkbox'][name='roleIds'][value='"+roleIds[i]+"']").attr("checked","checked");
        	}
			id=$("#id").val();
			originalEmail=$("#email").val();
		}
		
		var flag={"username":false,"password":false,"email":false};
		function usernameVerify(username){
			if(username==undefined||username==""||username==null){
				$("#msg1").attr("color","red").text("用户名不能为空");
				flag.username=false;
				return;
			}
			if(id!=""&&id!=null){//如果是修改不需要验证用户名
				$("#msg1").attr("color","green").text("");
				flag.username=true;
				return;
			}
			var url="${pageContext.request.contextPath}/appuser/appuser_usernameExist.action";
			$.ajax({
				type: "post",
				url: url,
				data: {"username":username},
				dataType:"html",
				success: function(msg){
					  if(msg=="success"){
						  $("#msg1").attr("color","green").text("");
						  flag.username=true;
					  }else{
						  $("#msg1").attr("color","red").text(msg);
						  flag.username=false;
					  }
				}
			}); 
		}
		function password1Verify(password){
			if(password==undefined||password==""||password==null){
				$("#msg4").attr("color","red").text("密码不能为空");
				flag.password=false;
			}else{
				$("#msg4").attr("color","green").text("");
				flag.password=true;
			}
		}
		function emailVerify(email){
			if(email==undefined||email==""||email==null){
				$("#msg2").attr("color","red").text("邮箱不能为空");
				flag.email=false;
				return;
			}
			if(!/^[a-zA-Z0-9_]+@([a-zA-Z0-9_]+.)+[a-zA-Z]+$/.test(email)){
				$("#msg2").attr("color","red").text("邮箱格式错误");
				flag.email=false;
				return;
			}
			if(id!=""&&id!=null&&originalEmail==$("#email").val()){//如果是修改,需要对比邮箱是否修改过再去验证是否存在
				$("#msg2").attr("color","green").text("");
				flag.email = true;
				return;
			}
			var url="${pageContext.request.contextPath}/appuser/appuser_emailExist.action";
			$.ajax({
				type: "post",
				url: url,
				data: {"email":email},
				dataType:"html",
				success: function(msg){
					  if(msg=="success"){
						  $("#msg2").attr("color","green").text("");
						  flag.email=true;
					  }else{
						  $("#msg2").attr("color","red").text(msg);
						  flag.email=false;
					  }
				}
			}); 
		}
		function submitVerify(obj){
			$("input[type!=radio][type!=checkbox][type!=button][type!=hidden][name!=realname]").each(function(){
				this.onblur();
			});
			return flag.username&&flag.password&&flag.email;
		}
        function sub_new(obj){
        	if(!submitVerify(obj)){
        		return;
        	}
        	obj.disabled=true;
			var roleIds = "";
			$("input[type=checkbox][name=roleIds][checked]").each(function(){
				roleIds=roleIds+","+$(this).val(); 
			});
			if(roleIds!=""){
				roleIds = roleIds.substr(1);
			}
			var usergroup_id = "";
			$("input[type=radio][checked]").each(function(){
				usergroup_id=$(this).val(); 
			});
			var admin = "0";
			if($("#admin").attr("checked")){
				admin = "1";
			}
			var url="${pageContext.request.contextPath}/admin/user_saveNew.action";
			if(id!=null&&id!=""){//如果有id说明是更新
				var url="${pageContext.request.contextPath}/admin/user_saveUpdate.action";
			}
			$.ajax({
				type: "post",
				url: url,
				data: {"id":id,"username":$("#username").val(),"roleIds":roleIds,"password":$("#password").val(),"email":$("#email").val(),"realname":$("#realname").val(),"usergroup_id":usergroup_id,"admin":admin},
				dataType:"html",
				success: function(msg){
			  		if(msg=="failed"){
						alert("保存失败!");
					}else{
						if("${successAlert}"=="1"){
							alert("保存成功!");
						}
						parent.afterSave();
					}
				}
			}); 
		}
    </script>
  </head>
  
  <body>
	   <table height="100%">
	    	<tr>
	    		<td>用户名</td>
	    		<td>
	    			<input id="username" name="username" type="text" value="${username}" <s:if test="id!=null">readonly</s:if>  onblur="usernameVerify(this.value);"/><font color="red" id="msg1">${msg1 }</font>
	    			<input id="id" name="id" type="hidden" value="${id}"/>
	    		</td>
	    		<td>密码</td>
	    		<td>
	    			<input id="password" name="password" type="password" value="${password}" onblur="password1Verify(this.value);"/><font color="red" id="msg4"></font>
	    		</td>
	    	</tr>
	    	<tr>
	    		<td>邮箱</td>
	    		<td>
	    			<input id="email" name="email" type="text" value="${email}" onblur="emailVerify(this.value);"/><font color="red" id="msg2">${msg2 }</font>
	    		</td>
	    		<td>真实名称</td>
	    		<td>
	    			<input id="realname" name="realname" type="text" value="${realname}"/>
	    		</td>
	    	</tr>
	    	<tr height="100%">
	    		<td>用户组</td>
	    		<td height="100%">
	    			 <div style="height: 100%;width: 100%;float: left;overflow: auto;">
						  <table border="1">
							  <s:iterator value="usergroupList" var="u">
							  	<tr><td>${name}</td><td><input type="radio" name="usergroup_id" value="${id}" <s:if test="id==default_usergroup_id">checked</s:if>/></td></tr>
							  </s:iterator>
						  </table>
					  </div>
	    		</td>
	    		<td>附加角色</td>
	    		<td height="100%">
	    			 <div style="height: 100%;width: 100%;float: left;overflow: auto;">
						  <table border="1">
							  <s:iterator value="roleList" var="r">
							  	<tr><td><s:if test="admin==1">后台</s:if><s:else>前台</s:else></td><td>${name}</td><td><input type="checkbox" name="roleIds" value="${id}"/></td></tr>
							  </s:iterator>
						  </table>
					  </div>
	    		</td>
	    	</tr>
	    	<tr>
	    		<td colspan="4" align="center"><input id="admin" type="checkbox" name="admin" value="1" <s:if test="admin==1">checked</s:if>/>管理员<input type="button" value="提交" onclick="sub_new(this);"/></td>
	    	</tr>
	    </table>
  </body>
</html>
